package com.example.newscrolling.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.example.newscrolling.enity.Movie;
import com.example.newscrolling.enity.Movie_d;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class UserDtDAO {
	private static String DB_NAME = "M_detail.db";
	private static final String TAG = "SQLITE";
	private static String sql = "SELECT * FROM M_DETAIL;";
	private Context ctx;

	public UserDtDAO(Context ctx) {
		super();
		this.ctx = ctx;
	}

	// copy Database
	private void copyDatabase(File dbFile) throws IOException {
		InputStream is = ctx.getAssets().open(DB_NAME);
		OutputStream os = new FileOutputStream(dbFile);

		byte[] buffer = new byte[1024];
		while (is.read(buffer) > 0) {
			os.write(buffer);
		}

		os.flush();
		os.close();
		is.close();
	}

	//開啟資料庫可讀取
	public SQLiteDatabase openReadDatabase() {
	
		File dbFile = ctx.getDatabasePath(DB_NAME);
		
		if (!dbFile.exists()) {
			try {
				File parentDir = new File(dbFile.getParent());
				if (!parentDir.exists()) {
					parentDir.mkdir();
				}
				copyDatabase(dbFile);
			} catch (IOException e) {
				throw new RuntimeException("Error creating source database", e);
			}
		}
		return SQLiteDatabase.openDatabase(dbFile.getPath(), null,SQLiteDatabase.OPEN_READONLY);
	}

	// 讀取資料庫內容放到ArrayList
	public List<Movie_d>getAll() {
		List<Movie_d> ret = new ArrayList<Movie_d>();

		 sql = "SELECT * FROM M_DETAIL;";
		SQLiteDatabase db = this.openReadDatabase();
		
		Cursor cursor = db.rawQuery(sql, null);

		if (cursor.moveToFirst()) {
			do {
				int id = cursor.getInt(0);
				String name = cursor.getString(1);
				String date = cursor.getString(2);
				String type = cursor.getString(3);
				String time = cursor.getString(4);
				String direct = cursor.getString(5);
				String actor = cursor.getString(6);
				String summary = cursor.getString(7);
				byte[] image = cursor.getBlob(8);
				Movie_d u = new Movie_d(id,name,date,type,time,direct,actor,summary,image);
				ret.add(u);
			} while (cursor.moveToNext());
		}
	
		cursor.close();
	
		db.close();
	
		return ret;
	}
	public Movie_d getUserById1(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		String sql = "SELECT * FROM M_DETAIL;";
		Cursor cursor = db.rawQuery(sql, null);
		Movie_d ret = null;
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
			
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String date = cursor.getString(2);
			String type = cursor.getString(3);
			String time = cursor.getString(4);
			//Log.i(TAG,time);
			String direct = cursor.getString(5);
			String actor = cursor.getString(6);
			String summary = cursor.getString(7);
			byte[] image = cursor.getBlob(8);
			ret = new Movie_d(id,name,date,type,time,direct,actor,summary,image);
		}
		return ret;
	}

	// SQlite取得產品圖片
	public Bitmap getBmp(int selectedIndex) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		Bitmap bmpout = null;
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(selectedIndex);
			byte[] image = cursor.getBlob(8);
			bmpout = BitmapFactory.decodeByteArray(image, 0, image.length);
		}
		cursor.close();
		db.close();

		return bmpout;

	}
	
	// SQlite取得電影名稱
	
	
	public String getName(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Log.i(TAG,"1");
		
		Cursor cursor = db.rawQuery(sql, null);
		
		Log.i(TAG,"2");
		if (cursor.moveToFirst()) {
			Log.i(TAG,"3");
			cursor.moveToPosition(position);
			Log.i(TAG,"4");
		}
		int nameColumnIndex = cursor.getColumnIndex("NAME");
		Log.i(TAG,"5");
		String name = nameColumnIndex+"";
		Log.i(TAG,"6");

		cursor.close();
		Log.i(TAG,"7");
		db.close();

		return name;
	}

	// SQlite取得日期
	public String getDate(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
		}
		int nameColumnIndex = cursor.getColumnIndex("DATE");
	     String date = nameColumnIndex+"";

		cursor.close();
		db.close();

		return date;
	}
	
	// SQlite取得類型
	public String getType(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
		}
		int nameColumnIndex = cursor.getColumnIndex("TYPE");
		String type = nameColumnIndex+"";

		cursor.close();
		db.close();

		return type;
	}
	
	// SQlite取得片長
	public String getTime(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
		}
		int nameColumnIndex = cursor.getColumnIndex("TIME");
		String time = nameColumnIndex+"";

		cursor.close();
		db.close();

		return time;
	}
	
	// SQlite取得導演
	public String getDirect(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
		}
		int nameColumnIndex = cursor.getColumnIndex("DIRECT");
		String direct = nameColumnIndex+"";

		cursor.close();
		db.close();

		return direct;
	}
	// SQlite取得演員
	
	public String getActor(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
		}
		int nameColumnIndex = cursor.getColumnIndex("ACTOR");
		String actor = nameColumnIndex+"";

		cursor.close();
		db.close();

		return actor;
	}
	
	//SQlite取得概要
	
	public String getSummary(int position) {
		SQLiteDatabase db = this.openReadDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst()) {
			cursor.moveToPosition(position);
		}
		int nameColumnIndex = cursor.getColumnIndex("ACTOR");
		String summary = nameColumnIndex+"";

		cursor.close();
		db.close();

		return summary;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}